<?php
  if(!defined(MIN_BYET_LENGTH))
  define("MIN_BYET_LENGTH", 200000); //~200kb
                            
  include_once "WebServices/Proxy.php";
  
  class CheckUrl extends Proxy {
    private $data;
    private $result;
    
    public function CheckUrl($url){
        $this->curl = curl_init();
  
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_HEADER, true);
        curl_setopt($this->curl, CURLOPT_NOBODY, true);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);
        //curl_setopt($this->curl, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 15);
        $this->data = CheckUrl::curl_redir_exec($this->curl);
  
        curl_close($this->curl);
    }
    
    private static function curl_redir_exec($ch)
    {
        static $curl_loops = 0;
        static $curl_max_loops = 10;
        
        if ($curl_loops++ >= $curl_max_loops)
        {
            $curl_loops = 0;
            return false;
        }
        $data = curl_exec($ch);
        //list($header, $data) = explode("\n\n", $data, 2);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($http_code == 301 || $http_code == 302)
        {
            $matches = array();
            preg_match('/Location:(.*?)\n/', $data, $matches);
            $url = @parse_url(trim(array_pop($matches)));
            if (!$url)
            {
                //couldn't process the url to redirect to
                $curl_loops = 0;
                return $data;
            }
            $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
            if (!$url['scheme'])
                $url['scheme'] = $last_url['scheme'];
            if (!$url['host'])
                $url['host'] = $last_url['host'];
            if (!$url['path'])
                $url['path'] = $last_url['path'];
            $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
            curl_setopt($ch, CURLOPT_URL, $new_url);
            //debug('Redirecting to', $new_url);
            return CheckUrl::curl_redir_exec($ch);
        } else {
            $curl_loops=0;
            return $data;
        }
    }
    
    public function search($a=null,$b=null){
        //echo $data;
        //Content-Length: 1635 Content-Type: text/html 
        //preg_match("/HTTP\/1\.[1|0]\s+(\d{3})/i",$data,$m);//[\w\s\d]*Content-Length:\s+(\d+)
        //print_r ($m);
        //return $this->result = null;//$this->data;
        //return $this->result = $this->data;
        if(!$this->data)
          return $this->result = 'Domain not found';
  
        preg_match_all("/HTTP\/1\.[1|0]\s+(\d{3})/i",$this->data,$m);
        if($m[1][sizeof($m[1])-1] != 200)
          return $this->result = 'Page not found: '.$m[1][sizeof($m[1])-1].' error code';
  
        //preg_match_all("/Content-Type:\s+([\w-\/]+)/i",$this->data,$m);
        //if($m[1][sizeof($m[1])-1] != 'audio/mpeg' && $m[1][sizeof($m[1])-1] != 'audio/x-mpeg' && $m[1][sizeof($m[1])-1] != 'application/octet-stream')
          //return $this->result = 'File not in mpeg layer 3 format (MP3): '.$m[1][sizeof($m[1])-1].' found instead';//."\n\n".$this->data;
  
        preg_match_all("/Content-Length:\s+([\d]+)/i",$this->data,$m);
        if($m[1][sizeof($m[1])-1] < MIN_BYET_LENGTH)
          return $this->result = $m[1][sizeof($m[1])-1].' bytes...File too small. Minimum length is ~200Kb';
  }
  
  public function sendResult(){
    if($this->result == null)
      $msg = '<proxy id="response">OK</proxy>';
    else $msg = '<proxy id="response">'.$this->result.'</proxy>';
    
    Proxy::send($msg);
  }
  
  public function result() {
     return $this->result;
  }
   
 }
?>
